package ipebg.mzk.provider.system.service.impl;

import ipebg.mzk.common.util.Page;
import ipebg.mzk.facade.entity.MsAdminRole;
import ipebg.mzk.facade.entity.MsAdminRoleAuthority;
import ipebg.mzk.facade.entity.MsAdminUserRole;
import ipebg.mzk.facade.system.service.PagerService;
import ipebg.mzk.facade.system.service.RoleService;
import ipebg.mzk.provider.system.dao.AdminRoleAuthorityDao;
import ipebg.mzk.provider.system.dao.MsAdminUserRoleDao;
import ipebg.mzk.provider.system.dao.RoleDao;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service("roleServiceImpl")
public class RoleServiceImpl implements RoleService {
	private static Log log = LogFactory.getLog(RoleServiceImpl.class);
	@Resource(name="roleDao")
	RoleDao roleDao;
	
	@Resource(name="msAdminUserRoleDao")
	MsAdminUserRoleDao msAdminUserRoleDao;
	
	@Resource(name="adminRoleAuthorityDao")
	AdminRoleAuthorityDao adminRoleAuthorityDao;
	
	@SuppressWarnings("rawtypes")
	@Resource(name = "pageServiceImpl")
	private PagerService  pageServiceImpls;

	@Override
	public Page getRoleList(MsAdminRole roleBean_1, int from, int length) throws Exception {
		Page roleList;
		String hql = "from MsAdminRole u where u.roleId is not null";
		if(roleBean_1 != null){
			if(roleBean_1.getRoleName() !=null && !roleBean_1.getRoleName().equals("")){
				hql += " and u.roleName like '%"+roleBean_1.getRoleName()+"%'";
			}
			if(roleBean_1.getRoleCode() !=null && !roleBean_1.getRoleCode().equals("")){
				hql += " and u.roleCode like '%"+roleBean_1.getRoleCode()+"%'";
			}		
		}			
		hql+=" order by u.roleCode asc";
		
		try {
			roleList = pageServiceImpls.findPageByhqlQuery(from, length, hql);
		} catch (Exception e) {
			throw e;
		}
		return roleList;
	}

	@Override
	@Transactional
	public void saveOrUpdate(MsAdminRole roleBean) {
		try {
			roleDao.saveOrUpdate(roleBean);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	@Transactional
	public boolean deleteRole(String roleId) throws Exception {
		boolean flag = false;
		try {
			//MsAdminRole roleBean=roleDao.findById(roleId);
			MsAdminRole roleBean= new MsAdminRole();
			roleBean.setRoleId(roleId);
			roleDao.delete(roleBean);
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return flag;
	}

	@Override
	public MsAdminRole findMsAdminRoleById(String roleId) {
		MsAdminRole roleBean=roleDao.findById(roleId);
		return roleBean;
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<MsAdminUserRole> findUserByRoleId(String roleId) throws Exception {
		List<MsAdminUserRole> userRoleBeanList = null;
		String hql="from MsAdminUserRole role where role.roleId = '"+roleId+"'";		
		System.out.println(hql);
		try {
			userRoleBeanList = msAdminUserRoleDao.queryForList(hql, null);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return userRoleBeanList;
	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly=true)
	public List<MsAdminRoleAuthority> findAuthorityByRoleId(String roleId) throws Exception {
		List<MsAdminRoleAuthority> resultList = null;
		String hql="from MsAdminRoleAuthority role where role.roleId = '"+roleId+"'";		
		try {
			resultList = adminRoleAuthorityDao.queryForList(hql, null);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return resultList;
	}

	@Override
	public MsAdminRole findMsAdminRoleByRoleCode(String roleCode) throws Exception {
		MsAdminRole role = null;
		String hql="from MsAdminRole role where role.roleCode = '"+roleCode+"'";		
		try {
			role = adminRoleAuthorityDao.findUnique(MsAdminRole.class,hql);
		} catch (Exception e) {
			e.printStackTrace();
			throw e;
		}
		return role;
	}

	
	@SuppressWarnings("unchecked")
	@Override
	public List<MsAdminRole> getAllRoleList(String[] orderBy) {
		// TODO Auto-generated method stub
		return roleDao.findAll(orderBy);
	}
		
}
